﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.ArticleManage
{
	public partial class ReleaseSetting : BasePage
	{
		private ModuleArticleId _identifier;
		private bool _isSetIdentifier = false;

		/// <summary>
		/// 当前修改信息的标识符。
		/// </summary>
		protected ModuleArticleId Identifier
		{
			get
			{
				if (!_isSetIdentifier)
				{
					_identifier = new ModuleArticleId();
					if (!String.IsNullOrEmpty(Request.QueryString["ArticleID"]))
						_identifier.ArticleId = Convert.ToInt32(Request.QueryString["ArticleID"]);

					if (!String.IsNullOrEmpty(Request.QueryString["ModuleID"]))
						_identifier.ModuleId = Convert.ToInt32(Request.QueryString["ModuleID"]);

					_isSetIdentifier = true;
				}

				return _identifier;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
				{
					InitData();
					Page.DataBind();
				}
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				ModuleArticle info = UCModuleArticle.Info;
				info.ModuleId = Identifier.ModuleId;
				info.ArticleId = Identifier.ArticleId;

				ModuleArticles.Setting(info);
				UpdateComplete();
			}
		}

		/// <summary>
		/// 初始化数据。
		/// </summary>
		public void InitData()
		{
			ModuleArticle info = ModuleArticles.GetItem(Identifier.ModuleId, Identifier.ArticleId);
			UCModuleArticle.Info = info;
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = ModuleArticles.GetSettingRight(ModuleArticles.GetItem(Identifier.ModuleId, Identifier.ArticleId));
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 更新操作完成，显示消息提示。
		/// </summary>
		public void UpdateComplete()
		{
			Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Setting));
		}
	}
}
